package com.study.es.service;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSONObject;
import com.study.es.bean.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserService {

    private static final String INDEX_NAME = "myuser";

    private static final String INDEX_TYPE = "_doc";

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * 新增一个对象
     *
     * @param userInfo
     */
    public void save(UserInfo userInfo) {

        IndexRequest indexRequest = new IndexRequest(INDEX_NAME);
        indexRequest.id(userInfo.getId());
        String param = JSONObject.toJSONString(userInfo);
        indexRequest.source(param, XContentType.JSON);

        try {
            IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
            log.info("response status:{},op:{}", indexResponse.status().getStatus(), indexResponse.getResult().getOp());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 根据ID查询对象
     *
     * @param id
     * @return
     */
    public UserInfo get(String id) {
        GetRequest getRequest = new GetRequest(INDEX_NAME, id);
        try {
            GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
            if (getResponse.isExists()) {
                log.info("response isExists:{},source:{}", getResponse.isExists(), getResponse.getSource());
                Map<String, Object> map = getResponse.getSource();
                UserInfo userInfo = Convert.convert(UserInfo.class, map);
                return userInfo;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<UserInfo> list() {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices(INDEX_NAME);

        try {
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            if (searchResponse.status() != RestStatus.OK) {
                return null;
            }
            List<UserInfo> userInfos = new ArrayList<>();
            for (SearchHit hit : searchResponse.getHits()) {
                UserInfo userInfo = Convert.convert(UserInfo.class, hit.getSourceAsMap());
                userInfos.add(userInfo);
            }
            return userInfos;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
